<template>
	<view>
		<h2 style="text-align: center;margin-top: 10%;">登录</h2>
		<uni-card>
			<view style="display: flex;width: 100%;margin: 5% 0;">
				<text class="texts">用户名</text>
				<uni-easyinput class="inputtext" trim="all" v-model="username" placeholder="请输入用户名"></uni-easyinput>
			</view>
			<view style="display: flex;">
				<text class="texts">密码</text>
				<uni-easyinput class="inputtext" trim="all" v-model="password" placeholder="请输入密码"></uni-easyinput>
			</view>
			<button style="margin: 5%;" type="primary" @click="login()">登录</button>
			<button style="margin: 5%;" type="primary" @click="register()">去注册</button>
		</uni-card>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				baseurl:"http://localhost:8000/",
				username:"",
				password:"",
			}
		},
		methods: {
			register(){
				uni.navigateTo({
					url:"/pages/register/register"
				})
			},
			login(){
				console.log(this.username)
				console.log(this.password)
				if (!this.username||this.username==""){
					uni.showModal({
						title: '请输入用户名',
						showCancel: false,
						confirmText: '确定'
					});
					return;
				}
				if (!this.password&&this.password==""){
					uni.showModal({
						title: '请输入密码',
						showCancel: false,
						confirmText: '确定'
					});
					return;
				}
				uni.request({
				    url: this.baseurl+'user/'+this.username, 
					method:'GET',
				    success: (res) => {
						if (res.data.code=="0000"&&this.password==res.data.data.password){
							uni.showToast({
								title: '登录成功',
								icon:'success',
								duration: 1000,
								mask:true
							});
							setTimeout(() => {
								uni.navigateTo({
									url:"/pages/mainselect/mainselect"
								});
							}, 1000);
						}
						else{
							uni.showModal({
								title: '密码错误',
								showCancel: false,
								confirmText: '确定'
							});
						}
				    },
					fail() {
						console.log("fail");
						uni.showModal({
							title: '密码错误',
							showCancel: false,
							confirmText: '确定'
						});
					}
				});
			}
		},
		onShow() {
			this.username="";
			this.password="";
		}
	}
</script>

<style lang="scss">
	.texts{
		width: 20%;
		vertical-align: middle;
		margin-top: 2%;
		text-align: center;
		justify-content:center;
		
	}
	.inputtext{
		width: 80%;
	}
</style>
